package com.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Aspect
public class SysLog {

    //日志规则
    @Pointcut("execution(* com.service.*.*(..))")
    public void logRule(){}

    //方法执行使用的多少时间:Around
    //方法执行之前记录日志
    //@Before("SysLog.logRule()")
    public void beforeLog(){
        String d = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        System.out.println("start:"+ d);
    }

    //方法执行之后记录日志
    //@After("SysLog.logRule()")
    public void afterLog(){
        String d = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        System.out.println("end:"+ d);
    }

    @Around("SysLog.logRule()")
    public Object countTime(ProceedingJoinPoint joinPoint) throws Throwable{
        long start = System.nanoTime();

        Object ret = joinPoint.proceed();

        long end = System.nanoTime();

        System.out.println("方法执行时间:"+(end - start));

        return ret;
    }

}
